package com.open.capacity.finance.controller;


import com.open.capacity.commons.Result;
import com.open.capacity.finance.dto.PzImportDto;
import com.open.capacity.finance.dto.SourceExcelDto;
import com.open.capacity.finance.service.PzImportService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.*;

/**
 * 凭证
 *
 * @author
 * @email
 * @date 2020-06-05 17:40:41
 */
@Slf4j
@RestController
@RequestMapping("pz_imports")
@Api(tags = "凭证导入")
public class PzImportController {

    @Autowired
    private PzImportService pzService;


    /**
     * 导入预览
     */
    @RequestMapping("/preview")
    public Result<SourceExcelDto> importPreview(@RequestParam Map<String, Object> params, @RequestParam("file") MultipartFile multipartFile) {
        try {
            SourceExcelDto result = pzService.importPreview(multipartFile, params);
            return Result.succeed(result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.failed(e.getMessage());
        }
    }


    /**
     * 导入预览
     */
    @RequestMapping("/preview2")
    public Result importPreview2(@RequestParam Map<String, Object> params, @RequestParam("file") MultipartFile multipartFile) {
        try {
            String errorMsg = pzService.importPreview2(multipartFile, params);
            return Result.succeed(errorMsg, "ok");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.failed(e.getMessage());
        }
    }

    /**
     * 导入凭证
     */
    @RequestMapping("/save")
    //@LogAnnotation(module = "凭证", name = "导入")
    public Result save(@RequestParam Map<String, Object> params, @RequestBody @Valid List<PzImportDto> data) throws Exception {
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());

        return Result.succeed(1, "导入成功");
    }

    /**
     * 删除期初
     */
    @RequestMapping("/deleteQc")
    public Result deleteQc(@RequestParam Map<String, Object> params) throws Exception {
        pzService.deleteQc();
        return Result.succeed(1, "删除期初成功");
    }

}
